package com.fosss.band.service.impl;

import com.fosss.band.entity.User;
import com.fosss.band.mapper.FanConcertMapper;
import com.fosss.band.mapper.UserMapper;
import com.fosss.band.service.FanService;
import com.fosss.band.utils.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: fosss
 * Date: 2023/12/27
 * Time: 20:40
 * Description:
 */
@Service
public class FanServiceImpl implements FanService {

    @Resource
    private UserMapper userMapper;
    @Resource
    private FanConcertMapper fanConcertMapper;

    /**
     * 分页条件查询
     */
    @Override
    public Result getPageByCondition(Integer page, Integer size, String keyword) {
        PageHelper.startPage(page, size);
        //注意我们用user代替掉了fan
        List<User> list = userMapper.selectByCondition(keyword);
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return Result.ok().data("rows", pageInfo.getList()).data("total", pageInfo.getTotal());
    }

    /**
     * 删除乐迷（管理员才有的权力）
     */
    @Override
    public Result delete(Integer id) {
        userMapper.deleteById(id);
        return Result.ok();
    }

    /**
     * 根据演唱会id获取参加的歌迷信息
     *
     * @param id 演唱会id
     * @return
     */
    @Override
    public List<User> getFansByConcertId(Integer id) {
        //从参加表中获取参加该演唱会的歌迷id
        List<Integer> fanIds = fanConcertMapper.selectFanIdsByConcertId(id);
        //根据fanId查询封装歌迷信息
        List<User> list = fanIds.stream().map(fanId -> {
            return userMapper.selectById(fanId);
        }).collect(Collectors.toList());
        return list;
    }
}
